﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Web;
using ELGGAPI.Models;
using MySql.Data.MySqlClient;

namespace ELGGAPI.Data
{
    public class GameInstanceDataModels
    {
        public  int? playerid { get; set; }
        public int? gameid { get; set; }
        public string instanceid { get; set; }
        public int gamescore { get; set; }
        public long starttime { get; set; }
        public long endtime { get; set; }
        public long gamedate { get; set; }
        public long gamerecorddate { get; set; }
    }
    public class GameInstanceGraphModels
    {
        public double? spentperday{ get; set; }
        public double? multitimeperday{ get; set; }
        public IEnumerable<GameInstanceGraphDataModels> graphdata { get; set; }
    }
    public class GameInstanceGraphDataModels
    {
        public int? time { get; set; }
        public int? date { get; set; }
        //public int? gameid { get; set; }
        //public int? playerid { get; set; }
        
    }

   
    public class GameInstanceGraphDomain
    {
        public static GameInstanceGraphModels GetGameInstanceGraph( int pPlayerId, int pGameID, int? pMonth, int? pYear)
        {
            List<GameInstanceGraphDataModels> list = new List<GameInstanceGraphDataModels>();
            GameInstanceGraphModels Graph = new GameInstanceGraphModels();

            DataSet dataSet = new DataSet();
            using (MySqlConnection connection = new MySqlConnection(APIHelper.ConnectionString))
            {
                using (MySqlCommand selectCommand = new MySqlCommand(APIHelper.DBName + "my_getfrequencyofPlaygraph", connection))
                {
                    if (pMonth != 0)
                        selectCommand.Parameters.AddWithValue("pMonth", pMonth);
                    else
                        selectCommand.Parameters.AddWithValue("pMonth", DBNull.Value);

                    if (pYear == 0)
                        selectCommand.Parameters.AddWithValue("pYear", DBNull.Value);
                    else
                        selectCommand.Parameters.AddWithValue("pYear", pYear);

                    if (pGameID == 0)
                        selectCommand.Parameters.AddWithValue("pGameID", DBNull.Value);
                    else
                        selectCommand.Parameters.AddWithValue("pGameID", pGameID);

                    //if (pProviderID == 0)
                    //    selectCommand.Parameters.AddWithValue("pProviderID", DBNull.Value);
                    //else
                    //    selectCommand.Parameters.AddWithValue("pProviderID", pProviderID);

                    if (pPlayerId == 0)
                        selectCommand.Parameters.AddWithValue("pGameUserID", DBNull.Value);
                    else
                        selectCommand.Parameters.AddWithValue("pGameUserID", pPlayerId);


                    selectCommand.CommandType = CommandType.StoredProcedure;
                    using (MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(selectCommand))
                        ((DataAdapter)mySqlDataAdapter).Fill(dataSet);
                }
            }
            if (dataSet.Tables.Count > 0)
            {
                if (dataSet.Tables[0].Rows.Count > 0)
                {
                    DataRow drsum = dataSet.Tables[0].Rows[0];
                    Graph.spentperday =  double.Parse("0" + drsum["SpentPerDay"].ToString());
                    Graph.multitimeperday =  double.Parse("0" + drsum["MultiTimePerDay"].ToString());
                }
                foreach (DataRow dataRow in (InternalDataCollectionBase)dataSet.Tables[1].Rows)
                    list.Add(new GameInstanceGraphDataModels()
                    {
                        time = int.Parse("0" + dataRow["TimeInMinutes"].ToString()),
                        date = int.Parse("0" + dataRow["GameDate"].ToString()),
                        //gameid = int.Parse("0" + dataRow["gameid"].ToString()),
                        //playerid = int.Parse("0" + dataRow["GamePlayerId"].ToString())
                    });
                Graph.graphdata = list;
            }
            return Graph;
        }
       
    }

    public class GameInstanceDataDomainModel
    {
        
        public static string AddUpdateGameInstanceData(GameInstanceDataModels gameinstancedata)
        {
            string isInstanceDataGUID = "";


            using (MySqlConnection connection = new MySqlConnection(APIHelper.ConnectionString))
            {
                using (MySqlCommand mySqlCommand = new MySqlCommand(APIHelper.DBName + "my_gameInstanceDataInsertUpdate", connection))
                {
                    mySqlCommand.Parameters.AddWithValue("pGameInstanceGUID", gameinstancedata.instanceid);
                    mySqlCommand.Parameters.AddWithValue("pGamePlayerID", gameinstancedata.playerid);
                    mySqlCommand.Parameters.AddWithValue("pGameID", gameinstancedata.gameid);
                    mySqlCommand.Parameters.AddWithValue("pGameScore", gameinstancedata.gamescore);
                    mySqlCommand.Parameters.AddWithValue("pGameStartTime", gameinstancedata.starttime);
                    mySqlCommand.Parameters.AddWithValue("pGameEndTime", gameinstancedata.endtime);
                    mySqlCommand.Parameters.AddWithValue("pGameDate", gameinstancedata.gamedate);
                    mySqlCommand.Parameters.AddWithValue("pDateRecordAdded", gameinstancedata.gamerecorddate);
                    
                   
                    mySqlCommand.CommandType = CommandType.StoredProcedure;
                    mySqlCommand.Connection.Open();
                    MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                    while (mySqlDataReader.Read())
                    {

                        isInstanceDataGUID = mySqlDataReader["vgameinstancedataID"].ToString();

                    }
                    mySqlDataReader.Close();
                    connection.Close();
                }
            }
            return isInstanceDataGUID;
        }
        public static List<GameInstanceDataModels> GetGameInstanceDataList(int pProviderID, int pPlayerId, int pGameID)
        {
            List<GameInstanceDataModels> list = new List<GameInstanceDataModels>();
            DataSet dataSet = new DataSet();
            using (MySqlConnection connection = new MySqlConnection(APIHelper.ConnectionString))
            {
                using (MySqlCommand selectCommand = new MySqlCommand(APIHelper.DBName + "my_gameInstanceDatalist", connection))
                {
                    if (pProviderID!=0)
                        selectCommand.Parameters.AddWithValue("pProviderID", pProviderID);
                    else
                        selectCommand.Parameters.AddWithValue("pProviderID", DBNull.Value);

                    if (pPlayerId==0)
                        selectCommand.Parameters.AddWithValue("pGameUserID", DBNull.Value);
                    else
                        selectCommand.Parameters.AddWithValue("pGameUserID", pPlayerId);

                    if (pGameID == 0)
                        selectCommand.Parameters.AddWithValue("pGameID", DBNull.Value);
                    else
                        selectCommand.Parameters.AddWithValue("pGameID", pGameID);
                    

                    selectCommand.CommandType = CommandType.StoredProcedure;
                    using (MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(selectCommand))
                        ((DataAdapter)mySqlDataAdapter).Fill(dataSet);
                }
            }
            if (dataSet.Tables.Count > 0)
            {
                foreach (DataRow dataRow in (InternalDataCollectionBase)dataSet.Tables[0].Rows)
                    list.Add(new GameInstanceDataModels()
                    {
                        gameid = int.Parse("0" + dataRow["GameID"].ToString()),
                        instanceid = dataRow["GameInstanceGUID"].ToString(),
                        playerid = int.Parse("0" + dataRow["GamePlayerID"].ToString()),
                        gamescore = int.Parse("0" + dataRow["GameScore"].ToString()),
                        starttime = long.Parse("0" + dataRow["GameStartTime"].ToString()),
                        endtime = long.Parse("0" + dataRow["GameEndTime"].ToString()),
                        gamedate = long.Parse("0" + dataRow["GameDate"].ToString()),
                        gamerecorddate = long.Parse("0" + dataRow["DateRecordAdded"].ToString())
                       
                    });
            }
            return list;
        }
    }
  
}